package org.testis.services;

import org.testis.data.TestStepDataObjectSender;
import org.testis.data.parser.TestStepDopFactory;
import org.testis.data.parser.TestStepDop;
import org.testis.data.TestStepDataObject;
import org.testis.data.TestStepResultAsynchReciever;
import org.testis.data.TestStepResultReciever;
import org.testis.data.TestStepResultSynchReciever;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;


/**
 * A class to execute a test step according to given TestStepDataObject
 * This class recieves TestStepDataObject, sends it to test data object queue.
 * Also this class can bu used to wait response of synch test steps 
 * @author nurhat
 */
public class TestStepExecutor
{

    /***
     * Parse given test data object according to its module type
     * @see TestStepDop class and its extensions
     * @param data
     * @return
     */
    public TestStepDataObject parse(TestStepDataObject data)
    {
        TestStepDop parser =
            TestStepDopFactory.getParser(data);
        if (parser != null) {
            return parser.convert(data);
        }
        return null;
    }


    /**
     * sends TestStepDataObject to test data object queue.
     * @param dataObj
     * @return
     */
    public synchronized boolean send(TestStepDataObject dataObj,
                                     boolean parse)
    {
        TestStepDataObject obj;
        if (parse) {
            obj = parse(dataObj);
        }
        else {
            obj = dataObj;
        }
        LogSenderService.putMessageOnQueue("TestStepExecutor",
                                           AtofLogLevel.INFO,
                                           "Sending Message with id:" +
                                           dataObj.getMessageId() + "....",
                                           dataObj.getMessageId());
        TestStepDataObjectSender sender = TestStepDataObjectSender.getInstance();
        if (sender != null && obj != null) {
            sender.sendData(obj);
            LogSenderService.putMessageOnQueue("TestStepExecutor",
                                               AtofLogLevel.INFO,
                                               "Message with id:" +
                                               dataObj.getMessageId() + " sent",
                                               dataObj.getMessageId());
            return true;
        }
        LogSenderService.putMessageOnQueue("TestStepExecutor",
                                           AtofLogLevel.INFO,
                                           "Could not send message with id:" +
                                           dataObj.getMessageId(),
                                           dataObj.getMessageId());
        return false;
    }


    /**
     * Used to get a singleton synch/asynch test step result reciever.
     * @param isSynch
     * @return
     */
    public  static TestStepResultReciever getResultReciever(
        boolean isSynch)
    {
        TestStepResultReciever reciever;
        if (isSynch) {
            reciever =
                TestStepResultSynchReciever.getInstance();
        }
        else {
            reciever = TestStepResultAsynchReciever.getInstance();
        }
        return reciever;
    }
}
